package com.jotta.client.data;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;

import com.jotta.client.R;
import com.jotta.client.communicate.Configuration;

enum ModeOfDecodeFile {
	THUMBNAIL, IMAGE
}

public class PictureInfo extends FileInfo {
	public static int THUMBNAIL_SIZE = 35;
	public static int IMAGE_SIZE = 522;

	/*public PictureInfo() {
		super();
		setTypeOfFile(TypeOfFile.PICTURE);
	}*/

	public PictureInfo(FileInfo fileInfo) {
		super();
		this.setTitle(fileInfo.getTitle());
		this.currentRevionsNumber = fileInfo.currentRevionsNumber;
		this.pathOfItem = fileInfo.pathOfItem;
		this.pathOfParentFolder = fileInfo.pathOfParentFolder;
		this.fileSize = fileInfo.fileSize;
		this.mimeType = fileInfo.mimeType;
		this.typeOfFile = TypeOfFile.PICTURE;
	}

	@Override
	public int getImageResourceId() {
		return R.drawable.picture;
	}

	/**
	 * @return the isDownloaded
	 */
	public boolean isDownloaded() {
		String localFilePath = getLocalPathOfItem();
		File file = new File(localFilePath);

		if (file.exists() && file.length() == getFileSize()) {
			return true;
			// int latestRevionOfFile = DataAccess.getInstance()
			// .getLatestRevisionOfFile(localFilePath);
			// int currentRevisionOfFile = getCurrentRevionsNumber();
			// if (latestRevionOfFile != currentRevisionOfFile) {
			// return false;
			// } else {
			// return true;
			// }
		} else {
			return false;
		}
	}

	public Bitmap getThumbnail() {
		if (isDownloaded()) {
			File file = new File(getLocalPathOfItem());
			return decodeFile(file, ModeOfDecodeFile.THUMBNAIL);
		} else {
			return BitmapFactory.decodeResource(Configuration.getRes(),
					R.drawable.download);
		}
	}

	public Uri getImageUri() {
		return Uri.parse(getLocalPathOfItem());
	}

	public BitmapDrawable getImageDrawable() {
		File file = new File(getLocalPathOfItem());
		return new BitmapDrawable(decodeFile(file, ModeOfDecodeFile.IMAGE));
	}

	public Bitmap getImage() {
		if (isDownloaded()) {
			File file = new File(getLocalPathOfItem());
			return decodeFile(file, ModeOfDecodeFile.IMAGE);
		}
		return BitmapFactory.decodeResource(Configuration.getRes(), R.drawable.download);
	}

	// decodes image and scales it to reduce memory consumption
	private Bitmap decodeFile(File f, ModeOfDecodeFile mode) {
		try {
			// Decode image size
			BitmapFactory.Options o = new BitmapFactory.Options();
			o.inJustDecodeBounds = true;
			BitmapFactory.decodeStream(new FileInputStream(f), null, o);

			// The new size we want to scale to
			int REQUIRED_SIZE = IMAGE_SIZE;
			if (mode == ModeOfDecodeFile.THUMBNAIL)
				REQUIRED_SIZE = THUMBNAIL_SIZE;
			else if (mode == ModeOfDecodeFile.IMAGE)
				REQUIRED_SIZE = IMAGE_SIZE;

			// Find the correct scale value. It should be the power of 2.
			int width_tmp = o.outWidth, height_tmp = o.outHeight;
			int scale = 1;
			while (true) {
				if (width_tmp / 2 < REQUIRED_SIZE
						|| height_tmp / 2 < REQUIRED_SIZE)
					break;
				width_tmp /= 2;
				height_tmp /= 2;
				scale *= 2;
			}

			// Decode with inSampleSize
			BitmapFactory.Options o2 = new BitmapFactory.Options();
			o2.inSampleSize = scale;
			return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
		} catch (FileNotFoundException e) {
		}
		return null;
	}
}
